package com.example.server.service.impl;

import com.example.server.dto.OrderDto;
import com.example.server.repository.GenericRepository;
import com.example.server.service.GenericService;
import com.example.server.vo.OrderVo;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.io.Serializable;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public abstract class GenericServiceImpl<T extends Serializable, ID extends Number> implements GenericService<T, ID> {

    GenericRepository<T, ID> genericRepository;

    public abstract void setGenericRepository(GenericRepository<T, ID> genericRepository);

    @Override
    public int create(T t) {
        return genericRepository.create(t);
    }

    @Override
    public int removeById(ID id) {
        return genericRepository.removeById(id);
    }

    @Override
    public int modify(T t) {
        return genericRepository.modify(t);
    }

    @Override
    public T queryById(ID id) {
        return genericRepository.queryById(id);
    }

    @Override
    public List<T> queryAll() {
        return genericRepository.queryAll();
    }

    @Override
    public T queryOne(String sqlId, Object parameter) {
        return genericRepository.queryOne(sqlId, parameter);
    }

    @Override
    public List<T> queryList(String sqlId, Object parameter) {
        return genericRepository.queryList(sqlId, parameter);
    }

    @Override
    public int modifyOne(String sqlId, Object parameter) {
        return genericRepository.modifyOne(sqlId, parameter);
    }

    @Override
    public int queryNum(String sqlId, Object parameter) {
        return genericRepository.queryNum(sqlId, parameter);
    }

    @Override
    public double queryAmount(String sqlId, Object parameter) {
        return genericRepository.queryAmount(sqlId, parameter);
    }

    @Override
    public int removeByIds(int[] t) {
        return genericRepository.removeByIds(t);
    }

    @Override
    public int removeOne(String sqlId, Object parameters) {
        return genericRepository.removeOne(sqlId,parameters);
    }
    @Override
    public OrderVo getOrder(OrderDto orderDto){
        LocalDate begin = orderDto.getBeginTime();
        LocalDate end = orderDto.getEndTime();
        System.out.println(end);
        List<LocalDate> dataList = new ArrayList<>();
        dataList.add(begin);
        while (begin.compareTo(end) != 0) {
            begin = begin.plusDays(1);
            dataList.add(begin);
        }
        List<Double> turnOverList = new ArrayList<>();
        for (LocalDate date : dataList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            System.out.println(beginTime);
            System.out.println(endTime);
            Map map = new HashMap<>();
            map.put("begin", beginTime);

            map.put("end", endTime);
            map.put("sta", 4);

            Double turnOver = genericRepository.sumByMap(map);
            turnOver = turnOver == null ? 0 : turnOver;
            turnOverList.add(turnOver);
        }
        System.out.println(turnOverList);
        return OrderVo
                .builder()
                .dateList(StringUtils.join(dataList, ","))
                .turnOverList(StringUtils.join(turnOverList, ","))
                .build();
    }
}
